<?php defined('_JEXEC') or die;
/**
 * @package pmapi
 * @copyright Copyright (c)2014 Enrico Fagnoni
 * @license GNU General Public License version 3, or later
 *
 * @since 1.0.0
 */

// shortcut
function P($property,$value=null,$delimiter=null)
{
    if (!empty($value)){;
        $result = $property;
        switch ($delimiter) {
            case 'S': // string
                $result .=  ' "' . $value . '"';
                break;
            case 'T': // text
                $result .=  ' """' . $value . '"""';
                break;
            case 'U': // uri
                $result .=  ' <' . $value . '>';
                break;
            case 'C': // curie
                $result .=  ' :' . $value ;
                break;           
            default: 
                $result .=  ' ' . $value;
                break;
        }
        $result .=  " ; \n";
    } else {
        $result =  "#  no $property\n";
    }
    
    return $result;
}

?>
<?=$this->getTurtlePrefixes()?> 
@prefix : <#> .

<?=$this->getDefaultMetadata()?>
<>  foaf:primaryTopic :this ;
    void:inDataset <<?=UriHelper::mkResourceURI('users',null,'dataset')?>> ;
.
<?php if(empty($this->users)):?>
#### ANONYMOUS USER DESCRIPTION ##################
:this a gr:BusinessEntity ;
    rdfs:comment "<?=ApiHelper::_T('ANONYMOUS_USER_MESSAGE')?>";
    dcterms:subject o:shoppergroup_<?=ApiHelper::ANONYMOUS_SHOPPERGROUP_ID;?> ;
.
#### ENDOF ANONYMOUS USER DESCRIPTION #############
<?php return;endif?>

<?php foreach ($this->users as $user ): 
    // setup individualUserName
    $individualUserName = $user->first_name;
    if ($user->middle_name) $individualUserName .= ' '.$user->middle_name;
    if ($user->last_name) $individualUserName .= ' '.$user->last_name;
    $individualUserName = trim( $individualUserName);
    
    // infer the type of contact
    $userAddressType = empty($user->deliveryContacts)
        ?"# single point of contact\n"
        :"dcterms:subject ebactor:InvoicePoint, ebactor:AccountsReceivable , ebactor:TaxRepresentative ;\n" ;
?>
:<?=$user->id?> a gr:BusinessEntity ;
    <?=P('gr:taxID',            $user->vatid,'S')?>
    <?=P('gr:vatID',            $user->taxid,'S')?>
    <?=P('dcterms:identifier',  $user->customer_number,'S')?>
<?php if (!empty($user->shopper_groups)):?>
    dcterms:subject o:shoppergroup_<?=implode(' o:shoppergroup_', $user->shopper_groups)?> ;
<?php endif ?>
<?php if ($user->vcardKind=='Organization'): ?>
    a vcard:Organization;
    vcard:fn """<?=$user->company?>""" ;
    <?php if ($individualUserName): ?>vcard:related [
        a vcard:Kind ;
        vcard:fn """<?=$individualUserName?>""" ;
    ]<?php endif ?>    
<?php else :?>
    a vcard:Individual ;
    vcard:fn """<?=$individualUserName?>""" ;
    vcard:hasFN [
        a vcard:Name;
        <?=P('vcard:honorific-prefix',  $user->title,'T')?>
        <?=P('vcard:given-name',        $user->first_name,'T')?>
        <?=P('vcard:additional-name',   $user->middle_name,'T')?>
        <?=P('vcard:family-name',       $user->last_name,'T')?>
    ] ;
<?php endif?>

    # Accounting Point Of Contact:
<?php if (!empty($user->agentFor)):?>
    prov:actedOnBehalfOf :<?=implode(' :', $user->agentFor)?> ;
<?php endif ?>
<?php if (!empty($user->email)): ?>
    vcard:hasEmail [
        a vcard:Email ;
        <?=$userAddressType?>
        vcard:hasValue "<?=$user->email?>" 
    ] ;
<?php endif?>
<?php if (!empty($user->phone_1)): ?>
    vcard:hasTelephone [
        a vcard:Voice ;
        <?=$userAddressType?>
        vcard:hasValue "<?=$user->phone_1?>" 
    ] ;
<?php endif?>
<?php if (!empty($user->phone_2)): ?>
    vcard:hasTelephone [
        a vcard:Voice ;
        <?=$userAddressType?>
        vcard:hasValue "<?=$user->phone_2?>" 
    ] ;
<?php endif?>
<?php if (!empty($user->fax)): ?>
    vcard:hasTelephone [
        a vcard:Fax ;
        <?=$userAddressType?>
        vcard:hasValue "<?=$user->fax?>" 
    ] ;
<?php endif?>
<?php if (!empty($user->address_1) || !empty($user->address_2) || !empty($user->city)|| !empty($user->zip)): ?>
    vcard:hasAddress [
        a vcard:Address ;
        <?=$userAddressType?>
        <?=P('vcard:street-address',trim($user->address_1. ' '. $user->address_2),'T')?>
        <?=P('vcard:locality',$user->city,'S')?>
        <?=P('vcard:postal-code',$user->zip,'S')?>
        <?=P('vcard:country-name',$user->country,'S')?>
        <?=P('vcard:region',$user->state,'S')?>
    ] ;
<?php endif?>
    # Delivery Point:
<?php foreach ($user->deliveryContacts as $vcard ): ?>
<?php if (!empty($vcard->email)): ?>
    vcard:hasEmail [
        a vcard:Email ;
        dcterms:subject pm:DeliveryPoint ;
        vcard:hasValue "<?=$vcard->email?>" 
    ] ;
<?php endif?>
<?php if (!empty($vcard->phone_1)): ?>
    vcard:hasTelephone [
        a vcard:Voice ;
        dcterms:subject pm:DeliveryPoint ;
        vcard:hasValue "<?=$vcard->phone_1?>" 
    ] ;
<?php endif?>
<?php if (!empty($vcard->phone_2)): ?>
    vcard:hasTelephone [
        a vcard:Voice ;
        dcterms:subject pm:DeliveryPoint ;
        vcard:hasValue "<?=$vcard->phone_2?>" 
    ] ;
<?php endif?>
<?php if (!empty($vcard->fax)): ?>
    vcard:hasTelephone [
        a vcard:Fax ;
        dcterms:subject pm:DeliveryPoint ;
        vcard:hasValue "<?=$vcard->fax?>" 
    ] ;
<?php endif?>
<?php if (!empty($vcard->address_1) || !empty($vcard->address_2) || !empty($vcard->city)|| !empty($vcard->zip)): ?>
    vcard:hasAddress [
        a vcard:Address ;
        dcterms:subject pm:DeliveryPoint ;
        <?=P('vcard:locality',$vcard->city,'S')?>
        <?=P('vcard:postal-code',$vcard->zip,'S')?>
        <?=P('vcard:country-name',$vcard->country,'S')?>
        <?=P('vcard:region',$vcard->state,'S')?>
        <?=P('vcard:street-address',trim($vcard->address_1. ' '. $vcard->address_2),'T')?>
    ] ;
<?php endif?> 
<?php endforeach //($this->users->deliveryContacts as $vcard )?>
.

<?php endforeach; //($this->users as $user )?>
<?php 
if(JDEBUG){
    echo "#------------------ START DEBUGGING INFO -------------------------\n#";
    echo preg_replace("/\n/", "\n#", ApiHelper::debugInfo($this));
    echo "\n#------------------ END DEBUGGING INFO -------------------------\n";
}
?>
